package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	"ksd-social-api/modules/msg/model"
	"time"
)

type MessagePointMapper struct {
}

/**
 * 查询用户消息的时间点
 * @author feige
 * @date 2023-12-15
 * @version 1.0
 * @desc
 */
func (mapper *MessagePointMapper) GetMessageUserPoint(userId uint64, systemId uint64) *model.MessagePoint {
	db := orm.NewOrm()
	var model model.MessagePoint
	err := db.Raw("select * from xk_msg_point where user_id = ? and system_id = ?", userId, systemId).QueryRow(&model)
	if nil != err {
		return nil
	}
	return &model
}

/**
 * 修改消息的查看时间
 * @author feige
 * @date 2024-01-07
 * @version 1.0
 * @desc
 */
func (mapper *MessagePointMapper) SaveMessagePonit(userId uint64, systemId uint64, uuid string, stype int) bool {
	messageUserPoint := mapper.GetMessageUserPoint(userId, systemId)
	if messageUserPoint == nil {
		// 防止注册的时候没有保存记录
		mapper.SaveMessagePonitDefaultTime(userId, systemId, uuid, stype)
	} else {
		if stype == 1 {
			messageUserPoint.SystemLasttime = time.Now()
		}
		if stype == 2 {
			messageUserPoint.CourseLasttime = time.Now()
		}
		messageUserPoint.Uuid = uuid
		messageUserPoint.UserId = userId
		orm.NewOrm().Update(messageUserPoint)
	}
	return true
}

/**
 * 保存用户默认数据
 * @author feige
 * @date 2024-01-07
 * @version 1.0
 * @desc
 */
func (mapper *MessagePointMapper) SaveMessagePonitDefault(userId uint64, systemId uint64, uuid string) bool {
	messageUserPoint := model.MessagePoint{}
	messageUserPoint.UserId = userId
	messageUserPoint.Uuid = uuid
	messageUserPoint.SystemId = systemId
	orm.NewOrm().Insert(&messageUserPoint)
	return true
}

/**
 * 保存用户默认数据
 * @author feige
 * @date 2024-01-07
 * @version 1.0
 * @desc
 */
func (mapper *MessagePointMapper) SaveMessagePonitDefaultTime(userId uint64, systemId uint64, uuid string, stype int) bool {
	messageUserPoint := model.MessagePoint{}
	if stype == 1 {
		messageUserPoint.SystemLasttime = time.Now()
	}
	if stype == 2 {
		messageUserPoint.CourseLasttime = time.Now()
	}
	messageUserPoint.UserId = userId
	messageUserPoint.Uuid = uuid
	messageUserPoint.SystemId = systemId
	orm.NewOrm().Insert(&messageUserPoint)
	return true
}
